package com.example.crowdfunding.mvc.Controller;

import com.example.crowdfunding.constant.CrowdConstant;
import com.example.crowdfunding.entity.Authority;
import com.example.crowdfunding.entity.Role;
import com.example.crowdfunding.service.api.AdminService;
import com.example.crowdfunding.service.api.AuthorityService;
import com.example.crowdfunding.service.api.RoleService;
import com.example.crowdfunding.utils.ResultEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/assign")

public class AssignController {

    @Resource
    private AdminService adminService;
    @Resource
    private RoleService roleService;
    @Resource
    private AuthorityService authorityService;

    @RequestMapping(value = "/to/rolePage/{adminId}/{pageNum}/{keyword}.html")
    public String toAssignRolePage(
            @PathVariable("adminId") Integer adminId,
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable(value = "keyword", required = false) String keyword,
            ModelMap modelMap
    ) {

        // 1.查询已分配角色
        List<Role> assignedRoleList = roleService.getAssignedRole(adminId);

        // 2.查询未分配角色
        List<Role> unassignedRoleList = roleService.getUnassignedRole(adminId);

        // 3.存入模型
        modelMap.addAttribute("assignedRoleList", assignedRoleList);
        modelMap.addAttribute("unassignedRoleList", unassignedRoleList);
        return "assign-role";
    }

    @RequestMapping(value = "/do/assignRole.html")
    public String saveRelationship(
            @RequestParam("adminId") Integer adminId,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam(value = "keyword", required = false) String keyword,
            @RequestParam(value = "roleIds", required = false) List<Integer> roleIds,
            ModelMap modelMap
    ) {

        adminService.saveAdminRoleRelationship(adminId, roleIds);

        return "redirect:/admin/get/adminList.html?pageNum=" + pageNum + "&keyword=" + keyword;

    }

    @RequestMapping(value = "/get/allAuth.json")
    @ResponseBody
    public ResultEntity<List<Authority>> getAllAuth() {

        List<Authority> authorityList = authorityService.getAll();
        return ResultEntity.successWithData(authorityList, CrowdConstant.MESSAGE_GET_AUTH_SUCCESS);
    }

    @RequestMapping(value = "/get/assignedAuthId.json")
    @ResponseBody
    public ResultEntity<List<Integer>> getAuthByRoleId(@RequestParam("roleId") Integer roleId) {

        List<Integer> authIdList = authorityService.getAssignedAuthById(roleId);
        return ResultEntity.successWithData(authIdList, CrowdConstant.MESSAGE_GET_AUTH_SUCCESS);
    }

    @ResponseBody
    @RequestMapping(value = "/do/assignAuthToRole.json")
    public ResultEntity<String> saveRoleAuthRelationship(
            @RequestBody Map<String, List<Integer>> map
    ) {

        authorityService.saveRoleAuthRelationship(map);
        return ResultEntity.successWithoutData(CrowdConstant.MESSAGE_ASSIGN_AUTH_SUCCESS);
    }

}
